/*
 * Created by SharpDevelop.
 * User: Rogerio
 * Date: 12/01/2009
 * Time: 11:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.common
{
	/// <summary>
	/// Description of frmUpdates.
	/// </summary>
	public partial class frmUpdates : KryptonForm
	{
		public frmUpdates()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		private void BtnExecuteClick(object sender, EventArgs e)
		{
			string nameFile, newName;
			
			if (KryptonMessageBox.Show("Deseja realmente executar esse script?","Atualiza",
			                    MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
			{
				Database.ExecuteCommand(txtCommands.Text, true);
				if (Database.result.Status == Database.Status.SUCCESSFUL)
				{
					KryptonMessageBox.Show("Script executado com sucesso!");
					
					btnExecute.Enabled = false;
					nameFile = txtFileName.Text;
					newName  = Application.StartupPath+"\\scripts\\executed"+nameFile.Substring(nameFile.LastIndexOf("\\"))+DateTime.Now.ToString("ddMMyyyy-HHmm");
					if ( System.IO.File.Exists(nameFile) )
						System.IO.File.Move(nameFile, newName);
				}
				else
				{
					KryptonMessageBox.Show("Erro ao tentar executar sql!\n"+Database.result.ErrorMessage);
					return;
				}
			}
		}
		
		private void BtnSelectFileClick(object sender, EventArgs e)
		{
			openFileDialog1.DefaultExt			= ".jrfs";
			openFileDialog1.InitialDirectory 	= Application.StartupPath + "\\scripts\\new";
			openFileDialog1.Filter           	= "Script Files (*.jrfs)|*.jrfs|All Files (*.*)|*.*";
			openFileDialog1.FileName 			= "";
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				txtFileName.Text = openFileDialog1.FileName;
				
				if (System.IO.File.Exists(txtFileName.Text))
				{
					txtCommands.Text		= Util.getContentOfFile(txtFileName.Text);
					btnExecute.Enabled 		= true;
				}
			}
		}
		
		private void FrmUpdatesLoad(object sender, EventArgs e)
		{
			MaximumSize = Size;
			MinimumSize = Size;
		}
	}
}
